<?php
class UsersController 
	extends Zend_Controller_Action 
{
		
	public function indexAction() 
	{
		
		$pagination = $this->getHelper('Pagination');
		
		$params = array();
		
		$mapper = Vs_Factory::mapper( 'User' );
		
		$users = $mapper->getByParams( $params, $pagination->offset, $pagination->limit, $pagination->order );
		$totalItems = $mapper->countByParams( $params );
		
		$pagination->setItemsCount( $totalItems );
		
		$this->view->users = $users;
		
		
		if ( $this->getRequest()->isXmlHttpRequest()){
			$this->_helper->layout->disableLayout();
			$this->render('users');
		} 
		
	}
	
	public function createAction() 
	{
	    $userTypes = Vs_User::$types;
	    $userSexs = Vs_User::$sexs;
		
	    $mapper = Vs_Factory::mapper( 'City' );
	    $cities = $mapper->getByParams();
	    
	    $form = new Adm_Form_User(array(
			'userTypes' => $userTypes,
			'userSexs' => $userSexs,
			'cities' => $cities,
		));
		$form->setAction(		
			$this->_helper->url('create')
		);

		$mapper = Vs_Factory::mapper( 'User' );
		
		if ( $this->getRequest()->isPost()){

			$data = $this->getRequest()->getParams();
			
			if ( $form->isValid($data) ){			
				
				$user = new Vs_User( $data );
				
				$mapper->save( $user );
				
				$this->getHelper('ActionLog')->log();
				
				$this->getHelper('MyFlashMessenger')->addSuccess( "Usuario creado" );
				return $this->_helper->Redirector('index');
				
			} else {
				$form->populate( $data );			
			}	
		}	

		$this->view->form = $form;
	}
	
	public function updateAction() 
	{
	    $userId = (int) $this->_getParam('userId');
	    
	    $userTypes = Vs_User::$types;
	    $userSexs = Vs_User::$sexs;
		
	    $mapper = Vs_Factory::mapper( 'City' );
	    $cities = $mapper->getByParams();
		
	     $mapper = Vs_Factory::mapper( 'User' );
	    
		try{
			$user = $mapper->getById( $userId );
		} catch (Vs_Exception $e){		
			$this->getHelper('MyFlashMessenger')->addError( $e->getMessage() );
			$this->getHelper('Log')->exception( $e );
			return $this->_helper->Redirector('index');
		}
		
		$form = new Adm_Form_User(array(
			'userTypes' => $userTypes,
			'userSexs' => $userSexs,
			'cities' => $cities,
		));
		$form->setAction(		
			$this->_helper->url('create')
		);
		
		if($this->getRequest()->isPost()){
			
			$data = $this->getRequest()->getPost();
		
			if ( $form->isValid($data)) {

				
				$user->email = $data['email'];
				$user->name = $data['name'];
				
				if ( !empty( $data['password'] )){
					$user->setPassword( $data['password'] );
				}
				
				$mapper->save( $user );				
			
				$this->getHelper('ActionLog')->log();
				
				$this->getHelper('MyFlashMessenger')->addSuccess("Usuario {$userId} actualizado");
				return $this->_helper->Redirector('index');
				
			} else {			
				$form->populate($data);			
			}	
		}  else {			
			$form->populate( (array) $user );			
		}					
		
		$this->view->form = $form;
		$this->render('create');	
	}	

	public function deleteAction() 
	{
		$ids = explode("-", $this->getRequest()->getParam('ids'));
		
		$this->getHelper('ActionLog')->log();
		
		$mapper = Vs_Factory::mapper( 'User' );
		
		foreach ($ids as $userId){
			
			try {
				
				$user = $mapper->getById( $userId );
				$mapper->delete( $user );
				$this->getHelper('MyFlashMessenger')->addSuccess("Usuario {$userId} eliminado");
				
			}catch (Vs_Exception $e){
			
				$this->getHelper('MyFlashMessenger')->addError( $e->getMessage() );
				$this->getHelper('Log')->exception( $e );
			}
		
		} 
		
		return $this->_helper->Redirector('index');
	}
	
		
	
	
	
	
}